#!/bin/bash
# 此脚本幂等，可执行多次
set -x

devices=( vlan501 vlan502 vlan503 bond0)

for dev in "${devices[@]}"; do

	# 仅当 bond0 存在时才删除
	if ip link show $dev >/dev/null 2>&1; then
		echo "$dev 存在，正在删除..."
        	ip link set $dev down		  
      		ip link delete $dev
	fi

done

card0=enp1s0f0np0
card1=enp1s0f1np1


# 1. 等接口出现，最长 60 s
for dev in $card0 $card1; do
	for i in {1..60}; do
		[ -e /sys/class/net/$dev ] && break
		sleep 1
        done
	[ -e /sys/class/net/$dev ] || { echo "$dev not found"; exit 1; }
done


# 2. 彻底关掉 NM 托管（只关这两个口，不影响别的）
nmcli device set $card0 managed no
nmcli device set $card1 managed no

ip addr flush dev $card0

ip addr flush dev $card1

ip link set $card0 down

ip link set $card1 down

ip link add name bond0 type bond mode 802.3ad lacp_rate slow xmit_hash_policy layer3+4 miimon 100


ip link set $card0 master bond0

ip link set $card1 master bond0


ip link set bond0 up


ip link add link bond0 name vlan501 type vlan id 501
ip addr add 10.50.1.30/24 dev vlan501
ip link set vlan501 up 


ip link add link bond0 name vlan502 type vlan id 502
ip addr add 10.50.2.30/24 dev vlan502
ip link set vlan502 up 


ip link add link bond0 name vlan503 type vlan id 503
ip addr add 10.50.3.30/24 dev vlan503
ip link set vlan503 up 


